<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="css/common.css"/>
		<link rel="stylesheet" type="text/css" href="css/article_add.css"/>
		<link rel="stylesheet" href="layui/css/layui.css" media="all"/>
	</head>
	<body>
		<form action="" method="post" id="form" class="search_box">
			<div>
				<span>标题：</span>
				<input type="text" name="title" id="title" placeholder="请输入标题" />

				<input type="hidden" name="id" id="id" placeholder="请输入推广人员"/>
			</div>
			<div style="position: relative;">
				<span>作者：</span>
				<input type="text" autocomplete="off" name="layerName" id="layerName" class="lawyer_name" placeholder="请输入作者" />
				<ul class="name-list">
					<li>王</li>
				</ul>
			</div>
			<div>
				<span>关键字：</span>
				<input type="text" name="keyword" id="keyword" placeholder="请输入关键字" />
			</div>
			<!-- <div>
				<span>执业证号：</span>
				<input type="text" name="" placeholder="请输入执业证号" />
			</div> -->
			<!--<div style="line-height: 30px;height:auto;">
				<span>领域：</span>
				<div class="good_at">
					<label><input type="radio" name="expert" value="公司犯罪" checked>公司犯罪</label>
					<label><input type="radio" name="expert" value="海事犯罪">海事犯罪</label>
					<label><input type="radio" name="expert" value="职务犯罪">职务犯罪</label>
					<label><input type="radio" name="expert" value="金融犯罪">金融犯罪</label>
					<label><input type="radio" name="expert" value="毒品犯罪">毒品犯罪</label>
					<label><input type="radio" name="expert" value="毒品犯罪">毒品犯罪</label>
					<label><input type="radio" name="expert" value="网络犯罪">网络犯罪</label>
					<label><input type="radio" name="expert" value="走私犯罪">走私犯罪</label>
				</div>
			</div>-->
			<div style="line-height: 50px;">
				<span>类型：</span>
				<div class="good_at">
					<label><input type="radio" name="type" value="案例" checked>案例</label>
					<label><input type="radio" name="type" value="新闻">新闻</label>
				</div>
			</div>
			<div style="height: auto;">
				<span>内容：</span>
				<div style="flex: 1;">
					<textarea id="content" name="content" style="width: 100%;"></textarea>
				</div>
			</div>
			<button type="submit" class="sub" data-tip = "文章列表">提交</button>
			<button type="reset" class="reset" style="display: none;"></button>
		</form>
		
		<script src="js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/jquery.cookie.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript" src="layui/layui.js"></script>
		<script>
			//获得edit信息
				 var id= $.cookie("articleId");
					 if(id && id!=''){
						$.ajax({
										type: "GET",
										url: "/content/"+id,
										cache: false,
										contentType: 'application/json;utf-8',
										processData: false,
										success: function(data) {
												if (data.rel) {
													setInputValues(data.result);
													setRadioValues(data.result);
												}
										 }
						 });

					 }
					 $.cookie('articleId', '');
				layui.use(['element','form','upload','layedit'], function(){
					var element = layui.element,
					form = layui.form,
					upload = layui.upload,
					layedit = layui.layedit;

					layedit.set({
						uploadImage: {
							url: "content/upload",
							type: 'post'
						}
					});
					layedit.build('content'); //建立编辑器

						$('#form').submit(function (e) {
							$.ajax({
								type: "POST",
								url: "content/edit",
								data: new FormData($(this)[0]), // serializes the form's elements.
								cache:false,
								contentType: false,
								processData: false,
								success: function(data){
									if(data){
								if(data.rel){
									layui.use('layer', function() {
										var layer = layui.layer;
						
										layer.msg(data.msg, {
											icon: 6
										},function() {
											var text = $('.sub').attr('data-tip');
									        $(window.parent.$(".acrossTab li").each(function(){
									            if($(this).children('span').text() == text){
									                $(window.parent.$("aside").attr('data-tip',1));
									                $(this).click();
									            } else {
									                $(window.parent.$("aside li").each(function(){
									                    if ($(this).text() == text) {
									                        $(this).click();
									                    }
									                }));
									            }
									        }));
									        $('.reset').click();
										})
									})
								} else {
									layui.use('layer', function() {
										var layer = layui.layer;
										layer.msg(data.msg, {
											icon: 5
										})
									})
								}
								
							}else{
								layui.use('layer', function() {
									var layer = layui.layer;
									layer.msg('保存失败', {
										icon: 5
									})
								})
							}
								}
							});
							e.preventDefault();
        			});
				});
				
				
				$('.lawyer_name').on('input',function(){
					$('.name-list').hide();
					var name = $(this).val();
					if(name.length == 0){
						return false;
					}
					$.ajax({
						type:"get",
						url:"lawyer/list",
						data:{name : name},
						dataType: "json",
						success : function(data){
							var name_list = '';
						 	if(data.rows.length != 0){
						 		for(var i = 0; i< data.rows.length; i++){
						 			name_list += '<li>'+data.rows[i].name+'</li>'
						 		}
						 		
						 		$('.name-list').html(name_list).show();
						 		
						 		$('.name-list li').click(function(){
						 			
						 			$('.lawyer_name').val($(this).text());
						 			
						 			$(this).parent('ul').html('').hide();
						 		})
						 		
						 	} else {
						 		$('.name-list').html('').hide();
						 	}
						}
					});
				});


function setInputValues(data) {
    for (var p in data) {
        $("#" + p + "").val(data[p]);
    }
}

function setRadioValues(data) {
    for (var p in data) {
        if(p=="expert"||p=="type"){
					 $("input[name=" + p + "][value="+data[p]+"]").attr("checked",true);
        }
    }
}
		</script>
	</body>
</html>
